package com.czf.CurriculumVitae.control;

import com.czf.CurriculumVitae.bean.Admin;
import com.czf.CurriculumVitae.bean.Result;
import com.czf.CurriculumVitae.service.DBService;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * @author chen
 * @Description: 用户登录
 * @date 2021/3/25 16:40
 */
@WebServlet("/v1/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.    处理乱码,把编码字典设置为utf-8
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        //2.    接收前端发送过来的数据内容
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Admin admin = new Admin(username,password);
        Admin admin1 = DBService.userLogin(admin);

        Result result = null;
        if (admin1 != null && !StringUtils.isNullOrEmpty(admin1.getUsername()) && !StringUtils.isNullOrEmpty(admin1.getPassword())){
            result = new Result(0,"登录成功",admin1);
        }else {
            result = new Result(-1,"用户名或密码错误");
        }
        response.getWriter().append(result.toJSON());
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
